package com.hdoit.pisces.web.other.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.hdoit.pisces.logical.rel.model.Relative;
import com.hdoit.pisces.logical.rel.service.IRelativeService;
import com.tennetcn.component.web.BaseController;

 
@Controller
@RequestMapping(value="/admin/upload/")
public class MyFileUploaderExController extends BaseController {
	
	@Override
	public String getRoutePath() {
		return "areas/admin/upload/";
	}
	
	private final String myImageViewName="uploadImage";
	private final String myVideoViewName="uploadVideo";
	private final String imageUploadViewName="imageUpload";
	private final String videoUploadViewName="videoUpload";
	private final String resultViewName="uploadResult";
	private String name;
	
	private IRelativeService relativeService;
	
	public	IRelativeService getRelativeService() {
		return relativeService;
	}
	
	@Resource
	public void setRelativeService(IRelativeService relativeService){
		this.relativeService=relativeService;
	}
	
	@RequestMapping(value=myImageViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String image(String name){
		this.name = name;
		return view(myImageViewName);
	}
	
	@RequestMapping(value=myVideoViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String video(String name){
		this.name = name;
		return view(myVideoViewName);
	}
	
	
	 // submit to.
    @RequestMapping(value = imageUploadViewName , method = {RequestMethod.POST})
    public String uploadImageHandler(HttpServletRequest request, Model model,
            @RequestParam("file") MultipartFile[] files) {
    	
 
 
        // Root Directory.
        String uploadRootPath = "/opt/apache-tomcat-7.0.79/webapps/resources/"+name+"/images";
        System.out.println(name+"444444444444444444444444444");
        System.out.println("uploadRootPath=" + uploadRootPath+"111111111111111111111111111111");
 
        File uploadRootDir = new File(uploadRootPath);
//        System.out.println(11111);
        // Create directory if it not exists.
        if (!uploadRootDir.exists()) {
//        	System.out.println(22222);
            uploadRootDir.mkdirs();
        }
//        System.out.println(33333);
        List<File> uploadedFiles = new ArrayList<File>();
        for (int i = 0; i < files.length; i++) {
//        	System.out.println(44444);
            MultipartFile file = files[i];
 
            // Client File Name
            String name = file.getOriginalFilename();
//            System.out.println("Client File Name = " + name);
 
            if (name != null && name.length() > 0) {
                try {
                    byte[] bytes = file.getBytes();
 
                    // Create the file on server
                    File serverFile = new File(uploadRootDir.getAbsolutePath()
                            + File.separator + name);
 
                    // Stream to write data to file in server.
                    BufferedOutputStream stream = new BufferedOutputStream(
                            new FileOutputStream(serverFile));
                    stream.write(bytes);
                    stream.close();
        
                    uploadedFiles.add(serverFile);
//                    System.out.println("Write file: " + serverFile);
                } catch (Exception e) {
                    System.out.println("Error Write file: " + name);
                }
            }
        }
        model.addAttribute("uploadedFiles", uploadedFiles);
        model.addAttribute("k",name);
        return view(resultViewName);
    }
    
    
    // submit to.
    @RequestMapping(value = videoUploadViewName , method = {RequestMethod.POST})
    public String uploadVideoHandler(HttpServletRequest request, Model model,
            @RequestParam("file") MultipartFile[] files) {
    	
 
 
        // Root Directory.
        String uploadRootPath = "/opt/apache-tomcat-7.0.79/webapps/resources/"+name+"/videos";
        System.out.println(name+"444444444444444444444444444");
        System.out.println("uploadRootPath=" + uploadRootPath+"111111111111111111111111111111");
 
        File uploadRootDir = new File(uploadRootPath);
//        System.out.println(11111);
        // Create directory if it not exists.
        if (!uploadRootDir.exists()) {
//        	System.out.println(22222);
            uploadRootDir.mkdirs();
        }
//        System.out.println(33333);
        List<File> uploadedFiles = new ArrayList<File>();
        for (int i = 0; i < files.length; i++) {
//        	System.out.println(44444);
            MultipartFile file = files[i];
 
            // Client File Name
            String name = file.getOriginalFilename();
//            System.out.println("Client File Name = " + name);
 
            if (name != null && name.length() > 0) {
                try {
                    byte[] bytes = file.getBytes();
 
                    // Create the file on server
                    File serverFile = new File(uploadRootDir.getAbsolutePath()
                            + File.separator + name);
 
                    // Stream to write data to file in server.
                    BufferedOutputStream stream = new BufferedOutputStream(
                            new FileOutputStream(serverFile));
                    stream.write(bytes);
                    stream.close();
        
                    uploadedFiles.add(serverFile);
//                    System.out.println("Write file: " + serverFile);
                } catch (Exception e) {
                    System.out.println("Error Write file: " + name);
                }
            }
        }
        model.addAttribute("uploadedFiles", uploadedFiles);
        model.addAttribute("k",name);
        return view(resultViewName);
    }
    
}
	
